package com.binance.assetservice.web.controller;

import com.binance.assetservice.api.IUserAssetApi;
import com.binance.assetservice.data.models.UserAssetItem;
import com.binance.assetservice.vo.request.AssetTransferRequest;
import com.binance.assetservice.vo.request.AsssetBalanceRequest;
import com.binance.assetservice.vo.request.DeductionFeeRequest;
import com.binance.assetservice.vo.request.DribbletAssetToBNBRequest;
import com.binance.assetservice.vo.request.FreezeAssetRequest;
import com.binance.assetservice.vo.request.GetOneUserAssetRequest;
import com.binance.assetservice.vo.request.GetPrivateUserAssetRequest;
import com.binance.assetservice.vo.request.GetUserAssetRequest;
import com.binance.assetservice.vo.request.SyncBalanceRequest;
import com.binance.assetservice.vo.request.UserAssetTimeQuery;
import com.binance.assetservice.vo.request.UserAssetTransferBtcRequest;
import com.binance.assetservice.vo.request.UserAssetTransferDribbletBtcRequest;
import com.binance.assetservice.vo.request.UserNegativeAssetRequest;
import com.binance.assetservice.vo.request.WithdrawAssetFailRequest;
import com.binance.assetservice.vo.request.WithdrawAssetRequest;
import com.binance.assetservice.vo.request.WithdrawAssetSuccessRequest;
import com.binance.assetservice.vo.response.DribbletAssetToBNBResponse;
import com.binance.assetservice.vo.response.PagingResult;
import com.binance.assetservice.vo.response.UserAssetItemResponse;
import com.binance.assetservice.vo.response.UserAssetResponse;
import com.binance.assetservice.vo.response.UserAssetTransferBtcResponse;
import com.binance.assetservice.vo.response.UserAssetTransferDribbletBtcResponse;
import com.binance.assetservice.web.service.IUserAssetLogService;
import com.binance.assetservice.web.service.IUserAssetService;
import com.binance.master.models.APIRequest;
import com.binance.master.models.APIResponse;
import com.binance.master.utils.CopyBeanUtils;
import org.apache.curator.shaded.com.google.common.collect.Lists;
import org.javasimon.aop.Monitored;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@RestController
@Monitored
public class UserAssetController implements IUserAssetApi {

    @Autowired
    private IUserAssetService userAssetService;
    @Autowired
    private IUserAssetLogService userAssetLogService;

    @Override
    public APIResponse<UserAssetResponse> getPrivateUserAsset(
            @RequestBody @Validated APIRequest<GetPrivateUserAssetRequest> request) throws Exception {
        UserAssetResponse response = new UserAssetResponse();
        GetUserAssetRequest body = new GetUserAssetRequest();
        body.setUserIdList(Arrays.asList(request.getBody().getUserId()));
        body.setAsset(request.getBody().getAsset());
        PagingResult<UserAssetItem> pageResult = this.userAssetService.getUserAsset(body);
        if (pageResult.getRows() != null) {
            for (UserAssetItem userAsset : pageResult.getRows()) {
                response.getUserAssetList().add(CopyBeanUtils.copy(userAsset, UserAssetResponse.UserAsset.class));
            }
        }
        return APIResponse.getOKJsonResult(response);
    }

    @Override
    public APIResponse<PagingResult<UserAssetItemResponse>> getUserAsset(
            @RequestBody @Validated APIRequest<GetUserAssetRequest> request) throws Exception {
        PagingResult<UserAssetItemResponse> result = new PagingResult<UserAssetItemResponse>();
        PagingResult<UserAssetItem> pageResult = this.userAssetService.getUserAsset(request.getBody());
        result.setTotal(pageResult.getTotal());
        if (pageResult.getRows() != null) {
            List<UserAssetItemResponse> list = Lists.newArrayList();
            for (UserAssetItem userAsset : pageResult.getRows()) {
                list.add(CopyBeanUtils.copy(userAsset, UserAssetItemResponse.class));
            }
            result.setRows(list);
        }
        return APIResponse.getOKJsonResult(result);
    }

    @Override
    public APIResponse<UserAssetItemResponse> getOneUserAsset(
            @RequestBody @Validated APIRequest<GetOneUserAssetRequest> request) throws Exception {
        UserAssetItemResponse response = null;
        UserAssetItem userAsset = this.userAssetService.getOneUserAsset(request.getBody());
        if (userAsset != null) {
            response = CopyBeanUtils.copy(userAsset, UserAssetItemResponse.class);
        }
        return APIResponse.getOKJsonResult(response);
    }

    @Override
    public APIResponse<Void> withdrawAsset(@RequestBody @Validated APIRequest<WithdrawAssetRequest> request)
            throws Exception {
        this.userAssetService.withdrawAsset(request.getBody());
        return APIResponse.getOKJsonResult();
    }

    @Override
    public APIResponse<Void> withdrawAssetSuccess(
            @RequestBody @Validated APIRequest<WithdrawAssetSuccessRequest> request) throws Exception {
        this.userAssetService.withdrawAssetSuccess(request.getBody());
        return APIResponse.getOKJsonResult();
    }

    @Override
    public APIResponse<Void> withdrawassetFail(@RequestBody @Validated APIRequest<WithdrawAssetFailRequest> request)
            throws Exception {
        this.userAssetService.withdrawassetFail(request.getBody());
        return APIResponse.getOKJsonResult();
    }

    @Override
    public APIResponse<Void> asssetBalance(@RequestBody @Validated APIRequest<AsssetBalanceRequest> request)
            throws Exception {
        this.userAssetService.assetBalance(request.getBody());
        return APIResponse.getOKJsonResult();
    }

    @Override
    public APIResponse<Void> freezeAsset(@RequestBody @Validated APIRequest<FreezeAssetRequest> request)
            throws Exception {
        this.userAssetService.freezeAsset(request.getBody());
        return APIResponse.getOKJsonResult();
    }

    @Override
    public APIResponse<Void> syncBalance(@RequestBody @Validated APIRequest<SyncBalanceRequest> request)
            throws Exception {
        this.userAssetService.syncBalance(request.getBody());
        return APIResponse.getOKJsonResult();
    }

    @Override
    public APIResponse<Void> deductionFee(@RequestBody @Validated APIRequest<DeductionFeeRequest> request)
            throws Exception {
        this.userAssetService.deductionFee(request.getBody());
        return APIResponse.getOKJsonResult();
    }

    @Override
    public APIResponse<UserAssetTransferBtcResponse> userAssetTransferBtc(
            @RequestBody @Validated APIRequest<UserAssetTransferBtcRequest> request) throws Exception {
        return APIResponse.getOKJsonResult(this.userAssetService.userAssetTransferBtc(request.getBody()));
    }

    @Override
    public APIResponse<DribbletAssetToBNBResponse> dribbletAssetToBNB(
            @RequestBody @Validated APIRequest<DribbletAssetToBNBRequest> request) throws Exception {
        return APIResponse.getOKJsonResult(this.userAssetService.dribbletAssetToBNB(request.getBody()));
    }

    @Override
    public APIResponse<UserAssetTransferDribbletBtcResponse> userAssetTransferDribbletBtc(
            @RequestBody @Validated APIRequest<UserAssetTransferDribbletBtcRequest> request) throws Exception {
        return APIResponse.getOKJsonResult(this.userAssetService.userAssetTransferDribbletBtc(request.getBody()));
    }

    @Override
    public APIResponse<BigDecimal> queryAssetAmountByTime(
            @RequestBody @Validated APIRequest<UserAssetTimeQuery> request) {
        UserAssetTimeQuery query = request.getBody();
        BigDecimal amount =
                userAssetLogService.getAssetAmountByTime(query.getUserId(), query.getAsset(), query.getEndTime());
        return APIResponse.getOKJsonResult(amount);
    }

    @Override
    public APIResponse<Void> assetTransfer(@RequestBody @Validated APIRequest<AssetTransferRequest> request)
            throws Exception {
        this.userAssetService.assetTransfer(request.getBody());
        return APIResponse.getOKJsonResult();
    }

	@Override
	public APIResponse<Integer> queryNegativeAssetByUserId(@RequestBody @Validated APIRequest<UserNegativeAssetRequest> request) {
		return APIResponse.getOKJsonResult(this.userAssetService.getUserNegativeAsset(request.getBody()));
	}

    // @Override
    // public APIResponse<Void> processTradeForBackfillTrades(
    // @RequestBody @Validated APIRequest<ProcessTradeForBackfillTradesRequest> request) throws
    // Exception {
    // this.userAssetService.processTradeForBackfillTrades(request.getBody());
    // return APIResponse.getOKJsonResult();
    // }


}
